package com.c.ctgproject.ui.collect

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.MaterialHistoryAdapter
import com.c.ctgproject.adapter.guang.MaterialWarehouseAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityMaterialHistoryBinding
import com.c.ctgproject.entity.guang.MaterialWarehouseEntity
import com.c.ctgproject.entity.guang.MaterialWarehouseHistoryEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.getGson
import com.c.ctgproject.utils.handlerPosition

/**
 *作者:LiBW
 *创建日期: 2021/9/18
 *描述: 库存历史
 */
class MaterialHistoryActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityMaterialHistoryBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val id by lazy { intent.getIntExtra(MyConstants.ID, -1) }
    private val mList = ArrayList<MaterialWarehouseHistoryEntity.DataBean.ListBean>()
    private val mAdapter by lazy { MaterialHistoryAdapter(this, mList) }
    private var page=1

    override fun event() {
        mBinding.mCarRV.adapter = mAdapter
        mBinding.mCarRV.layoutManager = LinearLayoutManager(this)

    }

    override fun click() {
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    private fun getData() {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.materialHistory(id, page), MyObserverHandlerLoading(this, loading,mBinding.mRefresh) {
            MyLog.d("物料库存回调:$it")
            val data = getGson().fromJson(it, MaterialWarehouseHistoryEntity::class.java).data
            data?.list?.let { list ->
                if (page == 1) mList.clear()
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
                mBinding.mCarRV.handlerPosition(page)

            }
        })
    }

    override fun onResume() {
        super.onResume()
        page = 1
        getData()
    }
}